package com.suggestion.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.orderMgr.dao.OfFoodDAO;
import com.suggestion.bean.OfSuggestion;
import com.suggestion.bean.Suggestion;
import com.suggestion.biz.ISuggestion;
import com.suggestion.dao.ISuggestionDAO;
import com.usermgr.bean.OfUser;
import com.usermgr.dao.OfUserDAO;
/**
 * 评论管理的action
 * @author meishuangyan
 *
 */
public class SuggestionAction {
	private OfSuggestion ofSuggestion;
	private ISuggestion suggestion;
	private ISuggestionDAO suggestionDAO;
	private OfUserDAO ofUserDAO;
	
	
	/**
	 * 添加评论
	 * @return
	 */
	public String AddSuggestion(){
		String reslut="";
		ofSuggestion = new OfSuggestion();
		String ofUserId= ServletActionContext.getRequest().getSession().getAttribute("userId").toString();
		
		String OfContext=ServletActionContext.getRequest().getParameter("context");
		if(ofUserId ==null || ofUserId.equals("")){
			ServletActionContext.getRequest().setAttribute("msg", "您没有登录 .. 请您先登录...");
			reslut = "error";
		}else if(OfContext == null || OfContext.equals("")){
			ServletActionContext.getRequest().setAttribute("msg", "操作失败！！！评论内容不能为空。。");
			reslut = "error";
		}else if(OfContext.length()>1000){
			ServletActionContext.getRequest().setAttribute("msg", "操作失败！！！评论内容不能大于1000");
			reslut = "error";
		}else{
			//加入用户名Id
			long userId=Long.parseLong(ofUserId);
			ofSuggestion.setOfUserId(userId);
			//加入内容
			OfContext.trim();
			ofSuggestion.setOfContent(OfContext);
			//加入时间
			Date date=new Date();
			ofSuggestion.setOfTime(date);
			if(suggestion.AddSuggestion(ofSuggestion)){
			reslut = "success";
				}
		}
		return reslut;
	}
	
	/**
	 * 查看用户的前10条评论
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getAllSuggestionLimit(){
		String reslut="success";
		List<List> suggList=new ArrayList<List>();
		List<OfSuggestion> list = suggestion.getAllSuggestionLimit();
		for(int i=0;i<list.size();i++){
			OfSuggestion suggestion=list.get(i);
		OfUser user = ofUserDAO.selectByPrimaryKey(suggestion.getOfUserId());
			List sugg=new ArrayList();
			sugg.add(suggestion.getOfId());
			sugg.add(suggestion.getOfUserId());
			sugg.add(user.getOfName());
			sugg.add(suggestion.getOfContent());
			sugg.add(suggestion.getOfTime());
			suggList.add(sugg);
		}
	if(list!=null){
		ServletActionContext.getRequest().setAttribute("suggestionListLimit",suggList);
		reslut = "success";
		}
		return reslut;
	}
	
	
	/**
	 * 查看所有的用户评论
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getAllSuggestion(){
		String reslut = "success";
		List<Suggestion> suggList=	suggestion.getAllSuggestion();
		if(suggList!=null && suggList.size()>0){
			ServletActionContext.getRequest().setAttribute("suggestionList",suggList);
			reslut = "success";
		}
		return reslut;
	}
	
	
	
	/**
	 * 通过ID删除评论
	 * @return
	 */
	public String deleteSuggestionById(){
		String re = "";
		String userId=ServletActionContext.getRequest().getParameter("id");
		if(userId == null){
			re = "error";
		}else{
			long ofId=Long.parseLong(userId);
			int reslut=-1;
			reslut=suggestion.deleteSuggestion(ofId);
			if(reslut>0){
				re =  "success";
			}
		}
		
		return re;
	}
	public OfSuggestion getOfSuggestion() {
		return ofSuggestion;
	}

	public void setOfSuggestion(OfSuggestion ofSuggestion) {
		this.ofSuggestion = ofSuggestion;
	}
	
	
	public OfUserDAO getOfUserDAO() {
		return ofUserDAO;
	}

	public void setOfUserDAO(OfUserDAO ofUserDAO) {
		this.ofUserDAO = ofUserDAO;
	}

	public ISuggestion getSuggestion() {
		return suggestion;
	}

	public void setSuggestion(ISuggestion suggestion) {
		this.suggestion = suggestion;
	}

	public ISuggestionDAO getSuggestionDAO() {
		return suggestionDAO;
	}

	public void setSuggestionDAO(ISuggestionDAO suggestionDAO) {
		this.suggestionDAO = suggestionDAO;
	}
	
}
